package com.xuzimian.globaldemo.aliyun;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.live.model.v20161101.DescribeLiveStreamRecordContentRequest;
import com.aliyuncs.live.model.v20161101.DescribeLiveStreamRecordContentResponse;
import com.aliyuncs.live.model.v20161101.DescribeLiveStreamRecordIndexFileRequest;
import com.aliyuncs.live.model.v20161101.DescribeLiveStreamRecordIndexFileResponse;
import com.aliyuncs.live.model.v20161101.DescribeLiveStreamRecordIndexFilesRequest;
import com.aliyuncs.live.model.v20161101.DescribeLiveStreamRecordIndexFilesResponse;
import com.aliyuncs.live.model.v20161101.DescribeLiveStreamsOnlineListRequest;
import com.aliyuncs.live.model.v20161101.DescribeLiveStreamsOnlineListResponse;
import com.aliyuncs.profile.DefaultProfile;
import com.google.gson.Gson;
import org.junit.Test;

import java.util.Optional;

/**
 * @program: global-demo
 * @description: 阿里云直播相关api
 * @author: xzm
 * @create: 2019-04-18 11:00
 **/
public class StreamingOnlineDemo {

    private final static String accessKeyId = "LTAIgBYafLxzBIYk";
    private final static String accessSecret = "2iZsNMKVdiZ8w51ouZ3X1R4wsbUhPu";
    private final static String regionId = "cn-hangzhou";

    /**
     * 查看指定域名下（或者指定域名下某个应用）的所有正在推的流的信息。
     * 即正在直播的节目
     */
    @Test
    public void testDescribeLiveStreamsOnlineList() {
        IAcsClient client = getiAcsClient();

        DescribeLiveStreamsOnlineListRequest request = new DescribeLiveStreamsOnlineListRequest();
        request.setDomainName("live.yocaigs.com");
        request.setAppName("yocai");

        try {
            Optional<DescribeLiveStreamsOnlineListResponse> responseOpt = Optional.ofNullable(client.getAcsResponse(request));
            boolean isOnline = responseOpt.map(d -> d.getOnlineInfo())
                    .map(d -> d.stream().filter(e -> "1j82hnrk8tmp_00000000".equals(e.getStreamName())).findFirst().isPresent()).orElseGet(() -> false);

            System.out.println("是否在线直播中："+isOnline);
            System.out.println(new Gson().toJson(responseOpt.get()));
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            System.out.println("ErrCode:" + e.getErrCode());
            System.out.println("ErrMsg:" + e.getErrMsg());
            System.out.println("RequestId:" + e.getRequestId());
        }

    }


    /**
     * 查询录制内容：DescribeLiveStreamRecordConte
     * 阿里云OpenAPI Explorer URL :https://api.aliyun.com/?spm=5176.11122631.962077.3.3a433801jIEwjr#/?product=live&api=DescribeLiveStreamRecordContent
     */
    @Test
    public void testDescribeLiveStreamRecordConte() {
        IAcsClient client = getiAcsClient();

        DescribeLiveStreamRecordContentRequest request = new DescribeLiveStreamRecordContentRequest();
        request.setDomainName("live.yocaigs.com");
        request.setStartTime("2019-04-09T00:00:00Z");
        request.setEndTime("2019-04-11T00:00:00Z");
        request.setStreamName("1j81132amby9_00000000");
        request.setAppName("yocai");

        try {
            DescribeLiveStreamRecordContentResponse response = client.getAcsResponse(request);
            System.out.println(new Gson().toJson(response));
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            System.out.println("ErrCode:" + e.getErrCode());
            System.out.println("ErrMsg:" + e.getErrMsg());
            System.out.println("RequestId:" + e.getRequestId());
        }

    }

    /**
     * 查询单个录制索引文件
     */
    @Test
    public void testDescribeLiveStreamRecordIndexFile() {
        IAcsClient client = getiAcsClient();

        DescribeLiveStreamRecordIndexFileRequest request = new DescribeLiveStreamRecordIndexFileRequest();
        request.setRecordId("ss");
        request.setDomainName("live.yocaigs.com");
        request.setStreamName("1j81132amby9_00000000");
        request.setAppName("yocai");

        try {
            DescribeLiveStreamRecordIndexFileResponse response = client.getAcsResponse(request);
            System.out.println(new Gson().toJson(response));
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            System.out.println("ErrCode:" + e.getErrCode());
            System.out.println("ErrMsg:" + e.getErrMsg());
            System.out.println("RequestId:" + e.getRequestId());
        }

    }

    /**
     * 查询录制索引文件
     * 注意：EndTime 与 StartTime 间隔时间不能超过 4 天。
     */
    @Test
    public void testDescribeLiveStreamRecordIndexFiles() {
        IAcsClient client = getiAcsClient();

        DescribeLiveStreamRecordIndexFilesRequest request = new DescribeLiveStreamRecordIndexFilesRequest();
        request.setStartTime("2019-04-23T00:00:00Z");
        request.setEndTime("2019-04-23T11:13:00Z");

        request.setStreamName("1j812qs99hxe_00000000");
        request.setDomainName("live.yocaigs.com");
        request.setAppName("yocai");
        request.setOrder("desc");
        try {
            DescribeLiveStreamRecordIndexFilesResponse response = client.getAcsResponse(request);
            System.out.println(new Gson().toJson(response));
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            System.out.println("ErrCode:" + e.getErrCode());
            System.out.println("ErrMsg:" + e.getErrMsg());
            System.out.println("RequestId:" + e.getRequestId());
        }

    }


    private IAcsClient getiAcsClient() {
        DefaultProfile profile = DefaultProfile.getProfile(regionId, accessKeyId, accessSecret);
        return new DefaultAcsClient(profile);
    }
}
